package com.auerdi.ui.account;

import android.graphics.Paint;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.auerdi.R;
import com.auerdi.dialog.AddressDialog;
import com.auerdi.ui.account.evemt.RefleshAddressEvemt;
import com.auerdi.ui.account.view.NumView;
import com.auerdi.ui.account.view.TestInutTextView;
import com.auerdi.utils.Tool;
import com.example.egghttp.EggLog.L;
import com.example.egghttp.RetrofitClient;
import com.example.egghttp.retrofit.AdService;
import com.example.egghttp.retrofit.beans.BOb;
import com.example.egghttp.retrofit.beans.banner.DelData;
import com.example.egghttp.retrofit.beans.banner.ResultsData;
import com.example.egghttp.retrofit.beans.goods.Address;
import com.example.egghttp.retrofit.params.AddreddEditRequest;
import com.example.egghttp.retrofit.params.AddreddIDRequest;

import org.greenrobot.eventbus.EventBus;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;
import es.dmoral.toasty.Toasty;
import me.yokeyword.fragmentation_swipeback.SwipeBackFragment;
import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * Created by tse on 16/2/14.
 */
public class ADADDresssAddFragment extends SwipeBackFragment {

    Unbinder unbinder;
    @BindView(R.id.statusBar)
    View statusBar;
    @BindView(R.id.title_left)
    RelativeLayout titleLeft;
    @BindView(R.id.etName)
    TestInutTextView etName;
    @BindView(R.id.phone)
    NumView phone;
    @BindView(R.id.ability)
    TestInutTextView ability;
    @BindView(R.id.chckleft)
    ImageView chckleft;
    @BindView(R.id.btn_delete)
    View btnDelete;
    @BindView(R.id.btn_save)
    View btnSave;
    @BindView(R.id.btn_check)
    RelativeLayout btnCheck;
    @BindView(R.id.aa)
    TextView aa;
    @BindView(R.id.bb)
    TextView bb;
    @BindView(R.id.cc)
    TextView cc;


    private boolean isCheck = true;
    private String mProvinceID = "0";
    private String mCityId = "0";
    private String mDistricId = "0";

    private String mAddreddID = "";
    private String mMobile = "";
    private String mName = "";
    private String mAddreddDetail = "";

    private static Address mAddress;

    public static ADADDresssAddFragment newInstance(String AddreddID, String mobile, String name, String addreddDetail, Address address) {

        mAddress = address;

        Bundle args = new Bundle();
        args.putString("AddreddID", AddreddID);
        args.putString("mobile", mobile);
        args.putString("mName", name);
        args.putString("mAddreddDetail", addreddDetail);
        ADADDresssAddFragment fragment = new ADADDresssAddFragment();
        fragment.setArguments(args);
        return fragment;
    }


    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        mAddreddID = getArguments().getString("AddreddID", "");
        mMobile = getArguments().getString("mobile", "");
        mAddreddDetail = getArguments().getString("mAddreddDetail", "");
        mName = getArguments().getString("mName", "");


    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.ad_fragment_address_add, container, false);


        unbinder = ButterKnife.bind(this, view);
        View tpview = attachToSwipeBack(view);
        L.d("hcia", "mAddreddID:" + mAddreddID);

        return tpview;
    }


    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        phone.getEditText().setText(mMobile);
        etName.getEditText().setText(mName);
        ability.getEditText().setText(mAddreddDetail);


        L.d("hcia", "mAddress:" + mAddress);


        if (!TextUtils.isEmpty(mAddress.getProvince_name())) {
            aa.setText(mAddress.getProvince_name());
        }

        if (!TextUtils.isEmpty(mAddress.getCity_name())) {
            bb.setText(mAddress.getCity_name());
        }

        if (!TextUtils.isEmpty(mAddress.getDistrict_name())) {
            cc.setText(mAddress.getDistrict_name());
        }


        mProvinceID = mAddress.getProvince_id();
        mCityId = mAddress.getCity_id();
        mDistricId = mAddress.getDistrict_id();

//        etName.getEditText().setHint("某某");
//        phone.getEditText().setHint("手机");
//        ability.getEditText().setHint("地址");


        aa.getPaint().setFlags(Paint.UNDERLINE_TEXT_FLAG);
        bb.getPaint().setFlags(Paint.UNDERLINE_TEXT_FLAG);
        cc.getPaint().setFlags(Paint.UNDERLINE_TEXT_FLAG);
        aa.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                AddressDialog.newInstance("", new AddressDialog.AddreddListener() {
                    @Override
                    public void onAddRess(Integer id) {
                        L.d("hcia", "id:" + id);
                        mProvinceID = id + "";
                        mCityId = "0";
                        mDistricId = "0";
                    }

                    @Override
                    public void onAddRess(String address) {
                        L.d("hcia", "address:" + address);


                        aa.setText(address);
                        bb.setText("  市  ");
                        cc.setText("  区  ");

                    }
                }).show(getChildFragmentManager(), "");


            }
        });
        bb.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                L.d("hcia", "mProvinceID:" + mProvinceID);


                AddressDialog.newInstance(mProvinceID, new AddressDialog.AddreddListener() {

                    @Override
                    public void onAddRess(Integer id) {
                        L.d("hcia", "id:" + id);
//                        mProvinceID = id + "";
                        mCityId = id + "";
                        mDistricId = "0";
                    }

                    @Override
                    public void onAddRess(String address) {
                        L.d("hcia", "address:" + address);


//                        aa.setText(address);
                        bb.setText(address);
                        cc.setText("  区  ");

                    }
                }).show(getChildFragmentManager(), "");


            }
        });

        cc.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                L.d("hcia", "mCityId:" + mCityId);
                AddressDialog.newInstance(mCityId, new AddressDialog.AddreddListener() {

                    @Override
                    public void onAddRess(Integer id) {
                        L.d("hcia", "id:" + id);
                        mDistricId = id + "";
                    }

                    @Override
                    public void onAddRess(String address) {
                        L.d("hcia", "address:" + address);


//                        aa.setText(address);
                        cc.setText(address);

                    }
                }).show(getChildFragmentManager(), "");

            }
        });


        btnCheck.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                isCheck = !isCheck;
                chckleft.setImageResource(isCheck ? R.mipmap.check_ok : R.mipmap.check_no);
            }
        });


        etName.getEditText().setHint("某某");
        phone.getEditText().setHint("手机");
        ability.getEditText().setHint("地址");


        titleLeft.setOnClickListener(v -> pop());
        btnDelete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {


                RetrofitClient.getPDAClient().create(AdService.class).addressDel(new AddreddIDRequest(mAddreddID))              //获取Observable对象
                        .subscribeOn(Schedulers.newThread())
                        .observeOn(Schedulers.io())

                        .observeOn(AndroidSchedulers.mainThread())
                        .subscribe(new Subscriber<BOb<DelData>>() {
                            @Override
                            public void onCompleted() {

                            }

                            @Override
                            public void onError(Throwable e) {

                            }

                            @Override
                            public void onNext(BOb<DelData> addressListsBOb) {


                                L.d("hcia", "addressListsBOb:" + addressListsBOb);


                                if (addressListsBOb.getBody().getIs_del().equals("1")) {
                                    EventBus.getDefault().post(new RefleshAddressEvemt());

                                    pop();
                                } else {
//                                    Toasty.error(getActivity(),"")

                                    pop();

                                }


                            }
                        });

            }
        });


        btnSave.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {


                if (mProvinceID.equals("0")) {
                    Toasty.error(getActivity(), "请填写省份").show();
                    return;
                }

                if (mCityId.equals("0")) {
                    Toasty.error(getActivity(), "请填写市").show();
                    return;
                }

                if (mDistricId.equals("0")) {
                    Toasty.error(getActivity(), "请填写区").show();
                    return;
                }

                if (TextUtils.isEmpty(etName.getText())) {
                    Toasty.error(getActivity(), "请填写收货人").show();
                    return;
                }

                if (TextUtils.isEmpty(phone.getText())) {
                    Toasty.error(getActivity(), "请填写手机号码").show();
                    return;
                }

                if (!Tool.isMobileNumber(phone.getText())) {
                    Toasty.error(getActivity(), "手机号码格式不对").show();
                    return;
                }


                if (TextUtils.isEmpty(ability.getText())) {
                    Toasty.error(getActivity(), "请填写详细地址").show();
                    return;
                }


                RetrofitClient.getPDAClient().create(AdService.class).addressEdit(new AddreddEditRequest(mAddreddID, etName.getText(), mProvinceID, mCityId, mDistricId, "", ability.getText(), "", phone.getText(), isCheck ? "1" : "0"))              //获取Observable对象
                        .subscribeOn(Schedulers.newThread())
                        .observeOn(Schedulers.io())

                        .observeOn(AndroidSchedulers.mainThread())
                        .subscribe(new Subscriber<BOb<ResultsData>>() {
                            @Override
                            public void onCompleted() {

                            }

                            @Override
                            public void onError(Throwable e) {

                            }

                            @Override
                            public void onNext(BOb<ResultsData> addressListsBOb) {


                                if (addressListsBOb.getBody().getIs_result().equals("1")) {

                                    EventBus.getDefault().post(new RefleshAddressEvemt());
                                    pop();
                                }


                            }
                        });


            }
        });


    }

    @Override
    public void onDetach() {
        super.onDetach();
//        mOnLoginSuccessListener = null;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    public interface OnLoginSuccessListener {
        void onLoginSuccess(String account);
    }

    @Override
    public void onSupportInvisible() {
        super.onSupportInvisible();
        hideSoftInput();
    }
}
